package com.itheima.tlias08_14.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.tlias08_14.VO.PageVO;
import com.itheima.tlias08_14.dto.SelectStudentDTO;
import com.itheima.tlias08_14.dto.StatisticEmpGenderDTO;
import com.itheima.tlias08_14.entity.Student;
import com.itheima.tlias08_14.mapper.StudentMapper;
import com.itheima.tlias08_14.paramobject.InsertStudent;
import com.itheima.tlias08_14.paramobject.SelectByIdStudent;
import com.itheima.tlias08_14.paramobject.SelectStudentPO;
import com.itheima.tlias08_14.paramobject.UpdateStudentPO;
import com.itheima.tlias08_14.service.StudentService;
import com.itheima.tlias08_14.utils.ResultUtil;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class StudentServiceImp implements StudentService {
    @Resource
    StudentMapper mapper;

    @Override
    public ResultUtil selectStudent(SelectStudentPO sp) {
        Page page = PageHelper.startPage(sp.getPage(), sp.getPageSize());
        List<Student> students = mapper.selectStudent(sp);
        PageVO<Student> oVO = new PageVO<>();
        oVO.setTotal(page.getTotal());
        oVO.setRows(students);
        return ResultUtil.success(oVO);
    }

    @Override
    public int hatchDeleteById(List<Integer> ids) {
        int i = mapper.hatchDeleteById(ids);
        return i;
    }

    @Override
    public int insertStudent(InsertStudent student) {
        //插入前判断学号是否已经存在
        List<StatisticEmpGenderDTO> studentByNo = mapper.selectByNo(student.getNo());
        if(studentByNo != null && !studentByNo.isEmpty()){
            return 0;
        }
        int i = mapper.insertStudent(student);
        return i ;
    }

    @Override
    public SelectStudentDTO selectStudentById(Integer id) {
        return mapper.selectStudentById(id);
    }

    @Override
    public int updateStudent(UpdateStudentPO usPO) {
        //插入前判断学号是否已经存在
        List<StatisticEmpGenderDTO> studentByNo = mapper.selectByNo(usPO.getNo());
        //获取学号相同的数据
        if(studentByNo != null && !studentByNo.isEmpty()){
            //判断获取到的学号相同的数据是否与当前被修改的ID是否相同
            for (StatisticEmpGenderDTO s : studentByNo) {
                if(s.getValue() != usPO.getId()){
                    return 0;
                }
            }
        }
        int i = mapper.updateStudent(usPO);
        return i;
    }

    @Override
    public int updateScore(Integer id, Integer score) {
        int i = mapper.updateScore(id, score);
        return i;
    }
}
